package com.pri.chat.activity;

import android.animation.Animator;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Point;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.netease.nim.uikit.net.StatusBarUtil;
import com.pri.chat.R;
import com.pri.chat.photo.HackyViewPager;
import com.pri.chat.photo.PhotoView;
import com.pri.chat.photo.PhotoViewAttacher;

import java.util.ArrayList;
import java.util.List;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

/**
 * 类说明： 加载本地或网络图片<br>
 * 类名：com.yitao.netpic.NetPicShowActivity <br>
 * 作者： 易涛 <br>
 * 时间：2015-7-17 上午9:54:16 <br>
 * 修改者：<br>
 * 修改日期：<br>
 * 修改内容：<br>
 */
public class NetPicShowActivity extends AppCompatActivity {
	/***************** 常用控件 ********************/
	private Intent intent = null;
	String str1 = null, str2 = null;
	/** 图片集浏览 */
	private HackyViewPager viewPager = null;
	private ImageView back = null;
	private TextView tv_content;

	private List<String> listPicPath = null;
	private List<String> listInstruction = null;
	private int item = 0;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		StatusBarUtil.setDarkMode(this);
		//禁止锁屏
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
		//透明导航栏
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
		setContentView(R.layout.activity_pic_show);
		init();
	}

	@SuppressWarnings("unchecked")
	private void init() {
		intent = getIntent();
		tv_content = (TextView) findViewById(R.id.tv_context);
		back = (ImageView) findViewById(R.id.topBack);
		back.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		Bundle bundle = intent.getExtras();
		if (bundle.containsKey("item")) {
			item = bundle.getInt("item");
		}
		listPicPath = (List<String>) bundle.getSerializable("listPath");
		listInstruction = null; //(List<String>) bundle.getSerializable("instruction");
		if (listInstruction == null || listInstruction.size() == 0) {
			tv_content.setVisibility(View.GONE);
		}else{
			tv_content.setVisibility(View.VISIBLE);
		}
		viewPager = (HackyViewPager) findViewById(R.id.viewpager);
		SamplePagerNetAdapter netAdapter = new SamplePagerNetAdapter(listPicPath, viewPager);
		viewPager.setAdapter(netAdapter);
		viewPager.setOffscreenPageLimit(2);
		viewPager.setCurrentItem(item);

		viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				if (listInstruction == null || listInstruction.size() == 0) {
					tv_content.setVisibility(View.GONE);
				} else {
					if ("".equals(listInstruction.get(arg0))) {
						tv_content.setVisibility(View.GONE);
					} else {
						tv_content.setText(listInstruction.get(arg0));
						tv_content.setVisibility(View.VISIBLE);
					}
				}
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
		});

	}

	OnItemClickListener clickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View view, final int position, long id) {
		}
	};

	class SamplePagerNetAdapter extends PagerAdapter {
		private List<String> picNameList = new ArrayList<String>();
		private Animator mCurrentAnimator;
		float startScale;
		HackyViewPager viewPager;
		Rect startBounds;
		float startScaleFinal;

		public SamplePagerNetAdapter(List<String> list, HackyViewPager hackyViewPager) {
			this.viewPager = hackyViewPager;
			this.picNameList = list;
		}

		@Override
		public int getCount() {
			if (null != picNameList && picNameList.size() > 0) {
				return picNameList.size();
			}
			return 0;
		}

		@Override
		public View instantiateItem(ViewGroup container, final int position) {
			// mIndex=position;
			final PhotoView photoView = new PhotoView(container.getContext());
			Glide.with(container.getContext()).load(picNameList.get(position)).into(photoView);
			container.addView(photoView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
			photoView.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
				public void onPhotoTap(View paramAnonymousView, float paramAnonymousFloat1, float paramAnonymousFloat2) {
					if (mCurrentAnimator != null) {
						mCurrentAnimator.cancel();
					}
					onBackPressed();
				}
			});

			return photoView;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View) object);
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			return view == object;
		}

		public boolean getScaleFinalBounds(int position) {
			View childView = viewPager.getChildAt(position);
			startBounds = new Rect();
			final Rect finalBounds = new Rect();
			final Point globalOffset = new Point();
			try {
				childView.getGlobalVisibleRect(startBounds);
			} catch (Exception e) {
				return false;
			}
			startBounds.offset(-globalOffset.x, -globalOffset.y);
			finalBounds.offset(-globalOffset.x, -globalOffset.y);

			if ((float) finalBounds.width() / finalBounds.height() > (float) startBounds.width() / startBounds.height()) {
				startScale = (float) startBounds.height() / finalBounds.height();
				float startWidth = startScale * finalBounds.width();
				float deltaWidth = (startWidth - startBounds.width()) / 2;
				startBounds.left -= deltaWidth;
				startBounds.right += deltaWidth;
			} else {
				startScale = (float) startBounds.width() / finalBounds.width();
				float startHeight = startScale * finalBounds.height();
				float deltaHeight = (startHeight - startBounds.height()) / 2;
				startBounds.top -= deltaHeight;
				startBounds.bottom += deltaHeight;
			}
			startScaleFinal = startScale;
			return true;
		}
	}
}
